package solutions.ferpa.ztest.intf;

import br.com.telescope.adsrt.dom.DomFlag;
import br.com.telescope.adsrt.dom.DomId;
import br.com.telescope.adsrt.dom.DomInteger;
import br.com.telescope.adsrt.dom.DomText;
import br.com.telescope.adsrt.dom.DomTextLong;
import br.com.telescope.adsrt.dom.DomUpper;
import br.com.telescope.adsrt.dom.DomUrl;
import br.com.telescope.adsrt.lib.*;
import br.com.telescope.adsrt.lib.AdsException;
import br.com.telescope.adsrt.lib.Field;
import br.com.telescope.adsrt.lib.Form;
import br.com.telescope.sql.ModelQuery;
import br.com.telescope.t2.ServiceModel;
import br.com.telescope.t2.exception.ApplicationRuntimeException;
import br.com.telescope.t2.feature.DeleteFeature;
import br.com.telescope.t2.feature.ExportFeature;
import br.com.telescope.t2.feature.ImportFeature;
import br.com.telescope.t2.feature.InsertFeature;
import br.com.telescope.t2.feature.ListFeature;
import br.com.telescope.t2.feature.LookupFeature;
import br.com.telescope.t2.feature.MultiRowInsertFeature;
import br.com.telescope.t2.feature.MultiRowUpdateFeature;
import br.com.telescope.t2.feature.UpdateFeature;
import br.com.telescope.util.ObjectUtil;
import java.util.ArrayList;
import java.util.List;
import solutions.ferpa.ztest.ent.*;

/**
 * Cadastro de itens de menus.
 * <p>
 * Parte do cadastramento de menus onde os itens do menu são cadastrados.
 * <P>
 * Telescope origin: RTMENU.CAD_ITEM_MENU
 * <P>
 * Base entity: RTMENU.ITENS_MENUS
 * <P>
 * @author TELESCOPE - Build task T2 Web Interface Generator
 * <P>
 * @version 0.1
 */
public class CadItemMenu extends ServiceModel {
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    public static final String GLOBAL_NAME = "RTMENU.CAD_ITEM_MENU";
    
    @Override
    public String globalName() {
        return GLOBAL_NAME;
    }
    
    {
        setTitle("Cadastro de itens de menus");
    }
    
    protected EntItensMenus base;
    protected EntMenus lkp1;
    protected EntInterfacesSistemas lkp2;
    protected EntItensMenus rec; // NEED_TJW1_COMPATIBILITY
    
    // Service parameters
    private DomId pMenuId;
    
    // Record items
    private DomText aTitulo;
    private DomUrl aUrl;
    private DomTextLong aParametros;
    private DomFlag aPublico;
    private DomFlag aAtivo;
    private DomInteger aSequencia;
    private DomId aMenuId;
    private DomId aId;
    private DomId aInterfaceId;
    private DomId aSubMenuId;
    private DomUpper aSubMenu;
    private DomUpper aNomeInterface;
    
    // Query items
    
    @Override
    public String getBaseSql() {
        return "select BASE.*"
                + " , LKP1.MENU as SUB_MENU"
                + " , LKP2.NOME as NOME_INTERFACE"
                + " , LKP1.ID as Z_LKP1_ID"
                + " , LKP2.ID as Z_LKP2_ID"
                + " from ITENS_MENUS BASE"
                + " left join MENUS LKP1 on LKP1.ID = BASE.SUB_MENU_ID"
                + " left join INTERFACES_SISTEMAS LKP2 on LKP2.ID = BASE.INTERFACE_ID";
    }
    
    @Override
    public void initAttributes() {
        
        aTitulo = new DomText();
        aTitulo.context(ctx).name("TITULO").label("Título");
        
        aUrl = new DomUrl();
        aUrl.context(ctx).name("URL").label("URL");
        
        aParametros = new DomTextLong();
        aParametros.context(ctx).name("PARAMETROS").label("Parâmetros");
        
        aPublico = new DomFlag();
        aPublico.context(ctx).name("PUBLICO").label("Público");
        aPublico.setDefaultValue("N");
        
        aAtivo = new DomFlag();
        aAtivo.context(ctx).name("ATIVO").label("Ativo");
        aAtivo.setDefaultValue("S");
        
        aSequencia = new DomInteger();
        aSequencia.context(ctx).name("SEQUENCIA").label("Sequencia");
        
        aMenuId = new DomId();
        aMenuId.context(ctx).name("MENU_ID").label("Menu");
        
        aId = new DomId();
        aId.context(ctx).name("ID").label("Id");
        
        aInterfaceId = new DomId();
        aInterfaceId.context(ctx).name("INTERFACE_ID").label("Interface");
        
        aSubMenuId = new DomId();
        aSubMenuId.context(ctx).name("SUB_MENU_ID").label("Sub-menu");
        
        aSubMenu = new DomUpper();
        aSubMenu.context(ctx).name("SUB_MENU").label("Sub-menu");
        aSubMenu.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT MENU as valor"
                + " , MENU as pesquisar"
                + " , MENU as descricao"
                + " from MENUS LKP1)", "q"));
        aSubMenu.setAutocompleteLength(2);
        
        aNomeInterface = new DomUpper();
        aNomeInterface.context(ctx).name("NOME_INTERFACE").label("Nome da Interface");
        aNomeInterface.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT NOME as valor"
                + " , NOME as pesquisar"
                + " , NOME as descricao"
                + " from INTERFACES_SISTEMAS LKP2)", "q"));
        aNomeInterface.setAutocompleteLength(1);
        
    }
    
    @Override
    public void initParameters() {
        pMenuId = new DomId(ctx, "MENU_ID", "Menu");
        pMenuId.setRequired(true);
        pMenuId.setValue(ctx.getParameter("P_MENU_ID"));
        if (pMenuId.isNotNull()) {
            aMenuId.setValue(pMenuId.getValue());
        }
    }
    
    @Override
    public List<Field> getParameters() {
        if (allParameters == null) {
            allParameters = new ArrayList<Field>();
            allParameters.add(pMenuId);
        }
        return allParameters;
    }
    
    @Override
    public void copyBindParameters() {
        EntItensMenus record = getRecord();
        if (pMenuId.isNotNull()) {
            record.setMenuId(pMenuId.getValue());
        }
    }
    
    @Override
    public void retrieveBindParameters() {
        pMenuId.setValue(getRecord().valueOfMenuId());
    }
    
    @Override
    public String getQueryPkColumn() {
        return "BASE.ID";
    }
    
    @Override
    public void setId(String value) {
        this.aId.setValue(value);
    }
    
    @Override
    public EntItensMenus getRecord() {
        if (base == null) {
            base = new EntItensMenus(ctx);
            rec = base;
        }
        return this.base;
    }
    
    @Override
    public String getContextName() {
        return aTitulo.toText();
    }
    
    @Override
    public String getDefaultOrderBy() {
        return "SEQUENCIA, TITULO";
    }
    
    @Override
    public void translateInsertFks(Form form) {
        ModelQuery query;
        
        // SUB_MENU_ID
        try {
            if (form.hasFields("SUB_MENU")) {
                query = createQuery()
                        .select("LKP1.ID")
                        .from("MENUS", "LKP1")
                        .filter("LKP1.MENU", form.get("SUB_MENU"))
                        ;
                base.setSubMenuId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
        
        // INTERFACE_ID
        try {
            if (form.hasFields("NOME_INTERFACE")) {
                query = createQuery()
                        .select("LKP2.ID")
                        .from("INTERFACES_SISTEMAS", "LKP2")
                        .filter("LKP2.NOME", form.get("NOME_INTERFACE"))
                        ;
                base.setInterfaceId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    @Override
    public void translateUpdateFks(Form form) {
        ModelQuery query;
        
        // SUB_MENU_ID
        try {
            if (form.hasFields("SUB_MENU")) {
                query = createQuery()
                        .select("LKP1.ID")
                        .from("MENUS", "LKP1")
                        .filter("LKP1.MENU", form.get("SUB_MENU"))
                        ;
                base.setSubMenuId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
        
        // INTERFACE_ID
        try {
            if (form.hasFields("NOME_INTERFACE")) {
                query = createQuery()
                        .select("LKP2.ID")
                        .from("INTERFACES_SISTEMAS", "LKP2")
                        .filter("LKP2.NOME", form.get("NOME_INTERFACE"))
                        ;
                base.setInterfaceId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="EXPORT">
    public class FExport extends ExportFeature {
        
        @Override
        public void initForm() {
            addField(aTitulo, "view");
            addField(aNomeInterface, "view");
            addField(aSubMenu, "view");
            addField(aUrl, "view");
            addField(aParametros, "view");
            addField(aPublico, "view");
            addField(aAtivo, "view");
            addField(aSequencia, "view");
        }
        
        @Override
        public void preRecord() {
            try {
                apresentarLinkSubMenu();
            } catch (AdsException ex) {
                throw new ApplicationRuntimeException(ex);
            }
        }
        
        {
            setFeatureName("EXPORT");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="LIST">
    public class FList extends ListFeature {
        
        @Override
        public void initForm() {
            addField(aTitulo, "view");
            addField(aNomeInterface, "view");
            addField(aSubMenu, "view");
            addField(aUrl, "view");
            addField(aParametros, "view");
            addField(aPublico, "view");
            addField(aAtivo, "view");
            addField(aSequencia, "view");
        }
        
        @Override
        public void preRecord() {
            try {
                apresentarLinkSubMenu();
            } catch (AdsException ex) {
                throw new ApplicationRuntimeException(ex);
            }
        }
        
        {
            setFeatureName("LIST");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="IMPORT">
    public class FImport extends ImportFeature {
        
        @Override
        public void initForm() {
            aTitulo.setOptional(false);
            addField(aTitulo, "rqd");
            addField(aNomeInterface, "opt");
            addField(aSubMenu, "opt");
            addField(aUrl, "opt");
            addField(aParametros, "opt");
            addField(aPublico, "opt");
            aAtivo.setOptional(false);
            addField(aAtivo, "rqd");
            addField(aSequencia, "opt");
        }
        
        {
            setFeatureName("IMPORT");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="INSERT">
    public class FInsert extends InsertFeature {
        
        @Override
        public void initForm() {
            aTitulo.setOptional(false);
            addField(aTitulo, "rqd");
            addField(aNomeInterface, "opt");
            addField(aSubMenu, "opt");
            addField(aUrl, "opt");
            addField(aParametros, "opt");
            addField(aPublico, "opt");
            aAtivo.setOptional(false);
            addField(aAtivo, "rqd");
            addField(aSequencia, "opt");
        }
        
        {
            setFeatureName("INSERT");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="INSERT_MR">
    public class FInsertMr extends MultiRowInsertFeature {
        
        @Override
        public void initForm() {
            aTitulo.setOptional(false);
            addField(aTitulo, "rqd");
            addField(aNomeInterface, "opt");
            addField(aSubMenu, "opt");
            addField(aUrl, "opt");
            addField(aParametros, "opt");
            addField(aPublico, "opt");
            aAtivo.setOptional(false);
            aAtivo.setDefaultValue("S");
            addField(aAtivo, "rqd");
            addField(aSequencia, "opt");
        }
        
        {
            setFeatureName("INSERT_MR");
            setTitle("Novos");
            setActionTitle("Novos");
            setPrimary(false);
            setRows(10);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="UPDATE">
    public class FUpdate extends UpdateFeature {
        
        @Override
        public void initForm() {
            aTitulo.setOptional(false);
            addField(aTitulo, "rqd");
            addField(aNomeInterface, "opt");
            addField(aSubMenu, "opt");
            addField(aUrl, "opt");
            addField(aParametros, "opt");
            aPublico.setOptional(false);
            addField(aPublico, "rqd");
            aAtivo.setOptional(false);
            addField(aAtivo, "rqd");
            addField(aSequencia, "opt");
        }
        
        {
            setFeatureName("UPDATE");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="UPDATE_8504485">
    public class FUpdate8504485 extends UpdateFeature {
        
        @Override
        public void initForm() {
            aTitulo.setOptional(false);
            addField(aTitulo, "rqd");
            addField(aNomeInterface, "opt");
            addField(aSubMenu, "opt");
            addField(aUrl, "opt");
            addField(aParametros, "opt");
            aPublico.setOptional(false);
            addField(aPublico, "rqd");
            aAtivo.setOptional(false);
            addField(aAtivo, "rqd");
            addField(aSequencia, "opt");
        }
        
        {
            setFeatureName("UPDATE_8504485");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="DELETE">
    public class FDelete extends DeleteFeature {
        
        @Override
        public void initForm() {
            addField(aTitulo, "view");
        }
        
        {
            setFeatureName("DELETE");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="UPDATE_MR">
    public class FUpdateMr extends MultiRowUpdateFeature {
        
        @Override
        public void initForm() {
            aTitulo.setOptional(false);
            addField(aTitulo, "rqd");
            addField(aNomeInterface, "opt");
            addField(aSubMenu, "opt");
            addField(aUrl, "opt");
            addField(aParametros, "opt");
            aPublico.setOptional(false);
            addField(aPublico, "rqd");
            aAtivo.setOptional(false);
            addField(aAtivo, "rqd");
            addField(aSequencia, "opt");
        }
        
        {
            setFeatureName("UPDATE_MR");
            setPrimary(false);
            setRows(10);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="SUB_MENU_LOOKUP">
    public class FSubMenuLookup extends LookupFeature {
        
        @Override
        public void initForm() {
            aSubMenu.setEditable(true);
            addField(aSubMenu, "opt");
        }
        
        @Override
        public ModelQuery getLookupQuery() {
            return createQuery()
                    .select("LKP1.MENU as SUB_MENU")
                    .from("MENUS", "LKP1")
                    ;
        }
        
        {
            setFeatureName("SUB_MENU_LOOKUP");
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="NOME_INTERFACE_LOOKUP">
    public class FNomeInterfaceLookup extends LookupFeature {
        
        @Override
        public void initForm() {
            aNomeInterface.setEditable(true);
            addField(aNomeInterface, "opt");
        }
        
        @Override
        public ModelQuery getLookupQuery() {
            return createQuery()
                    .select("LKP2.NOME as NOME_INTERFACE")
                    .from("INTERFACES_SISTEMAS", "LKP2")
                    ;
        }
        
        {
            setFeatureName("NOME_INTERFACE_LOOKUP");
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="initFeatures">
    
    @Override
    protected void initFeatures() {
        addFeature(new FExport());
        addFeature(new FList());
        addFeature(new FImport());
        addFeature(new FInsert());
        addFeature(new FInsertMr());
        addFeature(new FUpdate());
        addFeature(new FUpdate8504485());
        addFeature(new FDelete());
        addFeature(new FUpdateMr());
        addFeature(new FSubMenuLookup());
        addFeature(new FNomeInterfaceLookup());
        super.initFeatures();
    }
    
    @Override
    public String getDefaultFeature() {
        return "EXPORT";
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Getters and Setters">
    
    public String getTitulo() {
        return aTitulo.getValue();
    }
    
    public void setTitulo(String value) {
        this.aTitulo.setValue(value);
    }
    
    public String getUrl() {
        return aUrl.getValue();
    }
    
    public void setUrl(String value) {
        this.aUrl.setValue(value);
    }
    
    public String getParametros() {
        return aParametros.getValue();
    }
    
    public void setParametros(String value) {
        this.aParametros.setValue(value);
    }
    
    public String getPublico() {
        return aPublico.getValue();
    }
    
    public void setPublico(String value) {
        this.aPublico.setValue(value);
    }
    
    public String getAtivo() {
        return aAtivo.getValue();
    }
    
    public void setAtivo(String value) {
        this.aAtivo.setValue(value);
    }
    
    public Integer getSequencia() {
        return aSequencia.getValue();
    }
    
    public void setSequencia(Integer value) {
        this.aSequencia.setValue(value);
    }
    
    public Long getMenuId() {
        return aMenuId.getValue();
    }
    
    public void setMenuId(Long value) {
        this.aMenuId.setValue(value);
    }
    
    @Override
    public Long getId() {
        return aId.getValue();
    }
    
    public void setId(Long value) {
        this.aId.setValue(value);
    }
    
    public Long getInterfaceId() {
        return aInterfaceId.getValue();
    }
    
    public void setInterfaceId(Long value) {
        this.aInterfaceId.setValue(value);
    }
    
    public Long getSubMenuId() {
        return aSubMenuId.getValue();
    }
    
    public void setSubMenuId(Long value) {
        this.aSubMenuId.setValue(value);
    }
    
    public String getSubMenu() {
        return aSubMenu.getValue();
    }
    
    public void setSubMenu(String value) {
        this.aSubMenu.setValue(value);
    }
    
    public String getNomeInterface() {
        return aNomeInterface.getValue();
    }
    
    public void setNomeInterface(String value) {
        this.aNomeInterface.setValue(value);
    }
    
    public Long getParameterMenuId() {
        return pMenuId.getValue();
    }
    
    public void setParameterMenuId(Long value) {
        pMenuId.setValue(value);
        aMenuId.setValue(value);
    }
    // </editor-fold>
    
    /**
     * Apresentar link para acessar o menu.
     * <p>
     * Quando existir um sub-menu deve apresentar um link para acessar o 
     * sub-menu.
     */
    private void apresentarLinkSubMenu() throws AdsException {
        // <user-code id="5537298" parentId="5537297" hashcode="-12494ef0" length="170">
        aSubMenu.prefix("").suffix("");
        if (aSubMenu.isNotNull()) {
            aSubMenu.prefix("<a href='CadMenu?Z_ACTION=UPDATE&A_ID="+rec.valueOfSubMenuId()+"'>").suffix("</a>");
        }
        // </user-code>
    }
    
    @Override
    public String getJsServiceFile() {
        return "ui/rtmenu/cad_item_menu.js";
    }
}
